
var folders;
    
    
    Ext.onReady(function() {
   
      



    });

    function ChannelIsInFolder(cahnnelElement) {
        return (cahnnelElement.parent().id.indexOf("folder") >= 0)
    
    }

    function DeleteChannel(channelName) {
        var cahnnelEl= Ext.get("channel" + channelName);
        var parentEl=cahnnelEl.parent();
       cahnnelEl.hide();
       cahnnelEl.remove();
            if (Ext.get("UsersFeeds")!=parentEl && !parentEl.child("li")) 
                parentEl.child("*[class=deleteIcon]").show(true);

        }

        function DeleteFolder(folderName) {
            
            Ext.get("folder" + folderName).remove();
        
        
        }

function channelFolderChnaged(channelName, foldername) {

    Ext.get("TargetFolder").dom.value=foldername;
    Ext.get("CahnnelToChange").dom.value=channelName;
}



function ShowChannelEditDialog(channelName) {
    var foldersSelect = "<select id='folder' onchange=\"channelFolderChnaged('"+channelName+"',this.value)\">";
    Ext.get("TargetFolder").dom.value = "none";
    Ext.get("CahnnelToChange").dom.value = channelName;
    foldersSelect += "<option>none</option>";
    if(!folders)
    folders = Ext.query("*[id^=folder]");
    
     Ext.each(folders, function(arrayItem, index, allItems) {
           var folderName=Ext.get(arrayItem.id).child("*[class=folderName]").dom.value;
            foldersSelect += "<option>" + folderName + "</option>";
      });
       foldersSelect+="</select>";
        Ext.Msg.show({
            title: "Edit channel " + channelName,
            msg: "Choose folder for channel:"+foldersSelect,
            buttons: Ext.Msg.OKCANCEL,
            fn: changeChannelsFolder

            
        });


  }


  function changeChannelsFolder(btn, emptyhere) {

     if (btn = "ok"){
         var cahnnel = Ext.get("CahnnelToChange").dom.value;
         var folder = Ext.get("TargetFolder").dom.value;
      
         var cahnnelEl = Ext.get("channel" + cahnnel);
            if (folder != "none") {
                var folderUl = Ext.get("folder" + folder);
       
              //   var icon= folderUl.child("*[class=deleteIcon]");
              //   if(icon)
              //    icon.show();
           
               
           
         
                DeleteChannel(cahnnel);
               AddChannel(cahnnel, folderUl);
            }
            else {
                // if channel is in folder, adding it to root of tree (traget folder is none)
                if (cahnnelEl.parent().id.indexOf("folder") >= 0) {
                    DeleteChannel(cahnnel);
                    AddChannel(cahnnel, Ext.get("UsersFeeds"));
                }
             
            }
          
     }
    }

 

   function UpdateProfile(){
      ShowChannels();
      Ext.Msg.alert('','Profile updated.');

   }

   
   function ShowProfile() {
       Ext.get("UsersFeeds").hide(true);
       Ext.get("UsersFeeds").setDisplayed("none");
       Ext.get("ProfileLink").hide(true);
       Ext.get("ProfileLink").setDisplayed("none");
       Ext.get("ChannelsLink").show(true);
       Ext.get("ProfileTable").show(true);


   }

   function ShowChannels() {
       Ext.get("ProfileTable").hide();
       Ext.get("ProfileTable").setDisplayed("none");
       Ext.get("ChannelsLink").hide(true);
      
       Ext.get("ChannelsLink").setDisplayed("none");
       Ext.get("ProfileLink").show(true);
       Ext.get("UsersFeeds").show(true);

   }

   


   function ShowFeeds(title) {
       if (title == "") {
           Ext.Msg.alert('', 'Enter valid channel');
           return;
       }
     Ext.get("HelloTextDiv").hide(true);
     Ext.get("HelloTextDiv").setDisplayed("none");
     Ext.get("feeds").show(true);
    
     Ext.get("ChannelTitle").dom.innerHTML = title;
 }

 function RefreshFeed() {
     Ext.get("feedsContent").hide();
     Ext.get("feedsContent").show(true);
 }


 function CreateFolder() {
      Ext.MessageBox.show({
           title: 'New folder',
           msg: 'Folders name:',
           width:300,
           buttons: Ext.MessageBox.OKCANCEL,
           multiline: 20,
           fn: showResultText,
           animEl: 'CreateFolderLink'
       });
       ShowChannels();
       folders = Ext.query("*[id^=folder]");
 }


 function SaveChannel() {
     if (!Ext.get('channel' + Ext.get("ChannelTitle").dom.innerHTML))
       AddChannel(Ext.get("ChannelTitle").dom.innerHTML, Ext.get("UsersFeeds"));
   else
      Ext.Msg.alert('', 'Channel allready saved.');
 
 
 }




 function AddFolder(text) {
     var folder = "<ul id='folder" + text + "'><img width='23' height='22' src='images/openFolder.jpeg'/>";
     folder += "<a href='#'><img class='deleteIcon'  src='images/delete.gif' onclick=\"DeleteFolder('" + text + "')\" /></a>";
     folder += "<input type='hidden' class='folderName' value='" + text + "'/>";
     folder+="&nbsp;" + text + "</ul>";
     Ext.get("UsersFeeds").insertHtml("afterBegin", folder);
     folders = Ext.query("*[id^=folder]");

 }

 function AddChannel(text, parentElement) {
	
     var basicHtml = "id='channel" + text + "'>";
       basicHtml+="<a href='#'><img src='images/delete.gif' onclick=\"DeleteChannel('"+text+"')\" /></a>";
       basicHtml+="<a href='#'><img src='images/edit.gif' onclick=\"ShowChannelEditDialog('" + text + "')\" border='0'/></a>";
       basicHtml+="&nbsp;<a href='#' onclick=\"ShowFeeds('" + text + "')\">" + text + "</a>";
       var startTag="";
       var endTag = "";
       var target = "beforeEnd";
     if (parentElement != Ext.get("UsersFeeds")) {
         startTag = "<li ";
         endTag = "</li>";
        
        // parentElement.child("*[class=deleteIcon]").hide();
        // parentElement.child("*[class=deleteIcon]").setDisplayed("none");
     }
     else {
         startTag = "<ul ";
         endTag = "</ul>";
         target = "afterBegin";
    

      }
    
      parentElement.insertHtml(target,startTag + basicHtml + endTag);

 }
 
 function showResultText(btn, text){
     if (btn = "ok") {
         if (text != "")
             AddFolder(text);
        else
             Ext.Msg.alert('', 'Enter folders name.');
             
     
     }
    }
    
    
    function  BackToLogin(){
     Ext.get("newUserTable").hide(true);
     Ext.get("newUserTable").setDisplayed("none");
     Ext.get("loginTable").show(true);
   }

   function NewUserClick(){
     Ext.get("loginTable").hide(true);
     Ext.get("loginTable").setDisplayed("none");
     Ext.get("newUserTable").show(true);
   }